<?php
/**
 * Object representing a content interaction
 *
 * @package Concerto
 * @subpackage Models
 * @author Jason Raede <jason@torchdm.com>
 */
namespace application\models;
use application\utilities\Utility;
use application\models\DBObject;

class Interaction extends DBObject {
	
	/**
	 * ci_id
	 *
	 * @var int
	 */
	protected $id;
	
	/**
	 * ci_content_type
	 *
	 * @var int
	 */
	protected $contentType;
	
	/**
	 * ci_name
	 *
	 * @var string
	 */
	protected $name;
	
	/**
	 * ci_on_text
	 *
	 * @var string
	 */
	protected $onText;
	
	/**
	 * ci_off_text
	 *
	 * @var string
	 */
	protected $offText;
	
	/**
	 * ci_on_alert
	 *
	 * @var string
	 */
	protected $onAlert;
	
	/**
	 * ci_off_alert
	 *
	 * @var string
	 */
	protected $offAlert;
	
	/**
	 * If $id is provided, initialize object with its database data
	 *
	 * @param int $id
	 */
	public function __construct($id = null) {
		if($id) {
			$row = DB()->getRow("SELECT * FROM `cn_content_interactions` WHERE `ci_id`='$id'");
			$this->id = $id;
			$this->name = $row->ci_name;
			$this->contentType = $row->ci_content_type;
			$this->onText = $row->ci_on_text;
			$this->offText = $row->ci_off_text;
			$this->onAlert = $row->ci_on_alert;
			$this->offAlert = $row->ci_off_alert;
		}
	}
	
	/**
	 * Initialize object with data in $params
	 *
	 * @param array $params
	 */
	public function create($params) {
		if($params['name'] && $params['contentType']) {
			$this->name = $params['name'];
			$this->contentType = $params['contentType'];
			$this->onText = $params['onText'];
			$this->offText = $params['offText'];
			$this->onAlert = $params['onAlert'];
			$this->offAlert = $params['offAlert'];
		}
		else {
			Utility::triggerError('You must provide a name and content type for a user/content interaction.');
		}
	}
	
	/**
	 * Insert or update the interactions database row
	 *
	 * @return array DBObject status array.
	 */
	public function save() {
		$errors = array();
		// Make sure there's no interaction with the same name
		$sameName = DB()->getCount("SELECT `ci_id` FROM `cn_content_interactions` WHERE `ci_name`='$this->name' AND `ci_content_type`='$this->contentType' AND `ci_id` != '$this->id'");
		if($sameName) {
			$errors[] = 'You must give your interaction a unique name.';
			return array('status'=>'error', 'errors'=>$errors);
			
		}
		
		
		else {
			$dbFields = array('ci_name'=>$this->name, 'ci_content_type'=>$this->contentType, 'ci_on_text'=>$this->onText, 'ci_off_text'=>$this->offText, 'ci_on_alert'=>$this->onAlert, 'ci_off_alert'=>$this->offAlert);
			if($this->id) {
				DB()->update('cn_content_interactions', 'ci_id', $this->id, $dbFields);
				$id = $this->id;
			}
			else {
				DB()->insert('cn_content_interactions', $dbFields);
				$id = DB()->lastInsert();
			}
			return array('status'=>'success', 'id'=>$id);
		}
	}
	
	/**
	 * @deprecated Merged into save()
	 */
	public function add() {
		return $this->save();
	}
	
	/**
	 * Delete the interaction and any related rows from the database
	 */
	public function delete() {
		DB()->query("DELETE FROM `cn_content_interactions` WHERE `ci_id`='$this->id'");
		DB()->query("DELETE FROM `cn_user_content_relations` WHERE `ucr_interaction`='$this->id'");
		return array('status'=>'success');
	}
	
	

}
			

?>